using System;

namespace Geekees.App.Cashbook.Lib.Helper
{
	/// <summary>
	/// Summary description for FlashMessage.
	/// </summary>
	public class FlashMessage
	{
		public const string FLASH_MESSAGE_SESSION_KEY = "FlashMessageSessionKey";
		private static FlashMessageType _type = FlashMessageType.Normal;
		private static bool _staticFlash = false;
		private static int _holdTime = 5000;
		private static bool _displayCloseButton = true;
		private static int _displayCount = 0;

		public FlashMessage()
		{

		}

		public static void SetFlash( string msg )
		{
			SetFlash( msg, FlashMessageType.Normal, false, 5000, true );
		}

		public static void SetFlash( string msg, FlashMessageType type )
		{
			SetFlash( msg, type, false, 5000, true );
		}

		public static void SetFlash( string msg, FlashMessageType type, bool staticFlash, bool displayCloseButton )
		{
			SetFlash( msg, type, staticFlash, 5000, displayCloseButton );
		}

		public static void SetFlash( string msg, FlashMessageType type, int holdTime, bool displayCloseButton )
		{
			SetFlash( msg, type, false, holdTime, displayCloseButton );
		}

		public static void AddDisplayCount( int count )
		{
			if( count < 0 )
				return;

			_displayCount += count;
		}

		/// <summary>
		/// for displaying a message in a page
		/// to use:
		/// link the common.js
		/// add <%=	CNORBLL.Helper.FlashMessage.Message %> in the page where to display
		/// calling in .cs file: FlashMessage.SetFlash( "save successful!" );
		/// </summary>
		/// <param name="msg">message</param>
		/// <param name="type">flash type, normal|warning|error?</param>
		/// <param name="staticFlash">need fade</param>
		/// <param name="holdTime">time before fade</param>
		/// <param name="displayCloseButton">display an "x" for closing at the right side</param>
		private static void SetFlash( string msg, FlashMessageType type, bool staticFlash, int holdTime, bool displayCloseButton )
		{
			_type = type;
			_staticFlash = staticFlash;
			_holdTime = holdTime;
			_displayCloseButton = displayCloseButton;
			System.Web.HttpContext.Current.Session[FLASH_MESSAGE_SESSION_KEY] = msg;	
			
			_displayCount = 1;
			//AddDisplayCount(1);
		}

		public static string GetFlash()
		{
			if( System.Web.HttpContext.Current.Session[FLASH_MESSAGE_SESSION_KEY] == null )
				return string.Empty;

			string msg = System.Web.HttpContext.Current.Session[FLASH_MESSAGE_SESSION_KEY].ToString();
			string typeClass = "flash-normal";
			switch( _type )
			{
				case FlashMessageType.Warning:
					typeClass = "flash-warning";
					break;
				case FlashMessageType.Error:
					typeClass = "flash-error";
					break;
			}

//			string htmlCloseButton = _displayCloseButton ? "<div id='divFlashClose' class='flash-colse' style='position:absolute;right:0px;top:0px;'><a style='cursor:pointer;' onclick='this.parentNode.parentNode.style.display=\"none\";'>X</a></div>" : string.Empty;
//			string scriptFunction = _staticFlash ? "doStaticFlash();" : string.Format( "doFlash({0});", _holdTime );
//			string result = string.Format( "<div id='divFlash' class='flash-message {0}' style='position:relative;'>{1}{2}</div><script>{3}</script>",typeClass, msg, htmlCloseButton, scriptFunction);
			
			//string htmlCloseButton = _displayCloseButton ? "<div id='divFlashClose' class='flash-colse' style='position:absolute;right:0px;top:0px;'><a style='cursor:pointer;' onclick='this.parentNode.parentNode.style.display=\"none\";'>X</a></div>" : string.Empty;
			//string scriptFunction = _staticFlash ? "doStaticFlash();" : string.Format( "doFlash({0});", _holdTime );
			string result = string.Format( "<div id='divFlash' class='flash-message {0}' style='position:relative;'>{1}</div>",typeClass, msg );
			

			_displayCount --;
			if( _displayCount <= 0)
			{
				_displayCount = 0;
				System.Web.HttpContext.Current.Session[FLASH_MESSAGE_SESSION_KEY] = null;
			}

			return result;
		}

		public static string Message
		{
			get
			{
				return GetFlash();
			}
		}

		public enum FlashMessageType
		{
			Normal, Warning, Error
		}
	}
}
